<?php
namespace src;
use classlib\input;
use src\logic\Cadmin;
use classlib\core as C;
use classlib\core\lang;
use classlib\core\controller;
use src\logic\lupfile;
use classlib\core\config;
class Cslidepic extends controller {

	public function main() {
		$view   = C\view::inst('extjs');
		$userid = Cadmin::chkaccess($view);
		$dlang  = lang::inst('dd');
		$view->assign('use', $dlang->get('activity_use'));
		$view->assign('expire', $dlang->get('sh0007'));
		$view->assign_by_ref('title', Cadmin::$name);
		$view->display('live/activity/slidepic.ext.js');
	}

	public function dolist() {
		$view   = C\view::inst('json');
		Cadmin::chkaccess($view);
		list($start, $limit, $sort) = Cadmin::offset();
		$where  = array();
		$use    = input::post('use');
		if (is_numeric($use)) {
			$where['`use`'] = $use;
		}
		$expire = input::post('expire');
		if (is_numeric($expire) && $expire == 1) {
			$where[] = array('expire', config::$nowtime, \classlib\orm::OP_LT);
		} else if (is_numeric($expire) && $expire == 0) {
			$where[] = array('expire', config::$nowtime, \classlib\orm::OP_GT);
		}
		$orm    = C\model::load('pro.inc.activity');
		$total  = $orm->get_total($where);
		$list   = array();
		if ($total) {
			$list = $orm->get_list($start, $limit, $where, '*', $sort);
			$orm  = C\model::load('pro.pl.studio');
			foreach ($list as &$item) {
				$item['stime']  = date('Y-m-d H:i:s', $item['stime']);
				$item['expire'] = date('Y-m-d H:i:s', $item['expire']);
				$item['start']  = ($item['start'] > 0)? date('Y-m-d H:i:s', $item['start']):'';
				$apns = json_decode($item['apns'], true);
				$item['studioid'] = 0;
				$item['studio']   = '';
				if (isset($apns['studioid'])) {
					$item['studioid'] = $apns['studioid'];
					$studio = $orm->get_one($item['studioid']);
					$item['studio']   = empty($studio['title'])? $studio['author']:$studio['title'];
				}
			}
		}
		$view->assign_by_ref('total', $total);
		$view->assign_by_ref('dataset', $list);
		$view->display();
	}

	public function dodelete() {
		$view   = C\view::inst('json');
		Cadmin::chkaccess($view);
		$ids  = input::post('ids');
		$ids  = explode(',', $ids);
		if(!is_array($ids)) {
			$ids = array($ids);
		}
		$ids  = array_filter($ids, 'is_numeric');
		if(empty($ids)) {
			$view->assign_error('1', '请选择要删除的记录');
		}
		foreach ($ids as $id) {
			C\model::load('pro.inc.activity')->delete($id);
		}
		$view->display();
	}

	public function dosave() {
		$view   = C\view::inst('json');
		Cadmin::chkaccess($view);
		$id    = (int)input::post('id');
		$data  = array();
		$data['title']  = input::post('title');
		$data['link']   = input::post('link');
		$data['lng']    = input::post('lng');
		$data['rank']   = (int)input::post('rank');
		$data['use']    = (int)input::post('use');
		$dlang  = lang::inst('dd');
		$land   = $dlang->get('activity_use');
		if (!isset($land[$data['use']])) {
			$view->assign_error('use', '获取记录用途参数异常.');
		}
		$start          = input::post('start');
		$data['start']  = ($start)? strtotime($start):0;
		$data['expire'] = strtotime(input::post('expire'));
		$sid            = (int)input::post('sid');
		$apns           = array();
		if (is_numeric($sid) && $sid > 0) {
			$apns['studioid'] = $sid;
		}
		$data['apns']   = json_encode($apns);
		//验证上传的文件
		if(isset($_FILES['Filedata']) && is_uploaded_file($_FILES['Filedata']['tmp_name'])) {
			$src = lupfile::transfer($view, 'activity');
			if ($src) {
				$data['src'] = $src;
			}
		}
		$data['stime']   = C\config::$nowtime;
		if ($id > 0) {
			C\model::load('pro.inc.activity')->save($id, $data);
		} else {
			C\model::load('pro.inc.activity')->new_one($data);
		}
		$view->display();
	}
}